<?php
class TagController extends Controller
{
	public function init()
	{
		parent::init();
		$this->cs->registerScriptFile('/js/lib/admin/tag.js');
	}

	public function actionIndex()
	{
		$model = new Tag();
		if ($this->getParam('Tag')) {
			$model->attributes = $this->getParam('Tag');
		}
		$this->render('index',array(
			'model'=>$model,
		));
	}

	protected function performAjaxValidation($model)
	{
		if( $this->isPostRequest() && ('tag-form' == $this->getParam('ajax')) )
		{
			$model->scenario = 'ajax';
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}

	public function actionAdd()
	{
		$model = new Tag();
		$this->performAjaxValidation($model);
		if ($this->isPostRequest() && $this->getParam('Tag')) {
			$model->attributes = $this->getParam('Tag');
			if ($model->save()) {
				$this->redirect(array('/admin/tag/index'));
			}
		}
		$this->render('add',array(
			'model'=>$model,
		));
	}

	public function actionUpdate()
	{
		$model = Tag::model()->findByPk($this->getParam('id'));
		$this->performAjaxValidation($model);
		if ($this->isPostRequest() && $this->getParam('Tag')) {
			$model->attributes = $this->getParam('Tag');
			if ($model->save()) {
				$this->redirect(array('/admin/tag/index'));
			}
		}
		$this->render('edit',array(
			'model'=>$model,
		));
	}

	public function actionDelete()
	{
		if ($id = (int) $this->getParam('id')) {
			Tag::model()->deleteByPk($id);
		}
	}

	public function actionDeleteItems()
	{
		if ($ids = (array) $this->getParam('ids')) {
			Tag::model()->deleteAllByIds($ids);
		}
	}

}
